package com.ylsk.check.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ylsk.check.domain.dto.InsOperationUnitDTO;
import com.ylsk.check.domain.vo.InsOperationUnitVO;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.core.domain.AjaxResult;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.flowable.service.IWorkFlowService;
import com.ylsk.system.domain.FileSimple;
import com.ylsk.system.helper.FileHelper;
import com.ylsk.system.service.IMinioFileService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.check.mapper.InsOperationUnitMapper;
import com.ylsk.check.domain.InsOperationUnit;
import com.ylsk.check.service.IInsOperationUnitService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 高速运营单位Service业务层处理
 *
 * @author ysy
 * @date 2023-12-03
 */
@Service
public class InsOperationUnitServiceImpl implements IInsOperationUnitService
{
    @Autowired
    private InsOperationUnitMapper insOperationUnitMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;


    @Autowired
    private FileHelper fileHelper;

    @Autowired
    private IWorkFlowService workFlowService;

    @Autowired
    private IMinioFileService minioFileService;
    /**
     * 查询高速运营单位
     *
     * @param operationUnitId 高速运营单位主键
     * @return 高速运营单位
     */
    @Override
    public InsOperationUnitVO selectInsOperationUnitByOperationUnitId(Long operationUnitId)
    {
        InsOperationUnitVO insOperationUnitVO = insOperationUnitMapper.selectInsOperationUnitByOperationUnitId(operationUnitId);
        //设置logo附件信息
        final Long operationUnitLogo = insOperationUnitVO.getOperationUnitLogo();
        if(null != operationUnitLogo){
            List<FileSimple> fileSimpleList = minioFileService.getFileListByBusinessId(operationUnitLogo);
            insOperationUnitVO.setOperationUnitLogoFile(fileSimpleList);
        }

        //设置公司照片附件信息
        final Long operationUnitImage = insOperationUnitVO.getOperationUnitImage();
        if(null != operationUnitImage){
            List<FileSimple> fileSimpleList = minioFileService.getFileListByBusinessId(operationUnitImage);
            insOperationUnitVO.setOperationUnitImageFile(fileSimpleList);
        }
        return insOperationUnitVO;
    }

    /**
     * 查询高速运营单位列表
     *
     * @param insOperationUnit 高速运营单位
     * @return 高速运营单位
     */
    @Override
    public List<InsOperationUnit> selectInsOperationUnitList(InsOperationUnit insOperationUnit)
    {
        return insOperationUnitMapper.selectInsOperationUnitList(insOperationUnit);
    }

    /**
     * 新增高速运营单位
     *
     * @param insOperationUnitDTO 高速运营单位
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertInsOperationUnit(InsOperationUnitDTO insOperationUnitDTO)
    {
        final long operationUnitId = snowIdConfig.getSnowId();
        final long operationUnitLogo = snowIdConfig.getSnowId();
        final long operationUnitImage = snowIdConfig.getSnowId();

        InsOperationUnit insOperationUnit = new InsOperationUnit();
        BeanUtils.copyProperties(insOperationUnitDTO,insOperationUnit);
        insOperationUnit.setOperationUnitId(operationUnitId);
        insOperationUnit.setOperationUnitLogo(operationUnitLogo);
        insOperationUnit.setOperationUnitImage(operationUnitImage);
        int result = 0 ;

        //   保存附件数据
        String fileIds = insOperationUnitDTO.getFileIds();
        if(StringUtils.isNotEmpty(fileIds)){
            result += fileHelper.saveBusinessFileData(fileIds,operationUnitId);
        }
        //   公司logo
        String operationUnitLogoFile = insOperationUnitDTO.getOperationUnitLogoFile();
        if(StringUtils.isNotEmpty(operationUnitLogoFile)){
            result += fileHelper.saveBusinessFileData(operationUnitLogoFile,operationUnitLogo);
        }
        //  公司照片
        String operationUnitImageFile = insOperationUnitDTO.getOperationUnitImageFile();
        if(StringUtils.isNotEmpty(operationUnitImageFile)){
            result += fileHelper.saveBusinessFileData(operationUnitImageFile,operationUnitImage);
        }
        //  保存业务数据
        result += insOperationUnitMapper.insert(insOperationUnit);

        //发起流程
//        String processInstanceByKey = "constructionInspection";
//        String processInstanceId = "constructionInspection:1:4";
//        Map<String,Object> variables = new HashMap<>();
//        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();

//        try {
//            AjaxResult ajaxResult = workFlowService.startProcessInstanceById(processInstanceId,String.valueOf(operationUnitId),variables,insOperationUnitDTO,String.valueOf(userId));
//            if(ajaxResult.isSuccess()){
//                //获取流程实例id
//                Object data = ajaxResult.get("data");
//                System.out.println(data.toString());
//            }
//        }catch (Exception e ){
//            e.printStackTrace();
//        }

        return result;
    }

    /**
     * 修改高速运营单位
     *
     * @param insOperationUnitDTO 高速运营单位
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInsOperationUnit(InsOperationUnitDTO insOperationUnitDTO)
{

    final Long operationUnitId = insOperationUnitDTO.getOperationUnitId();
    final Long operationUnitLogo = insOperationUnitDTO.getOperationUnitLogo();
    final Long operationUnitImage = insOperationUnitDTO.getOperationUnitImage();
    InsOperationUnit insOperationUnit = new InsOperationUnit();
    BeanUtils.copyProperties(insOperationUnitDTO,insOperationUnit);
    int result = 0 ;

    //   保存附件数据
    String fileIds = insOperationUnitDTO.getFileIds();
    if(StringUtils.isNotEmpty(fileIds)){
        result += fileHelper.updateBusinessFileData(fileIds,operationUnitId);
    }
    //   公司logo
    String operationUnitLogoFile = insOperationUnitDTO.getOperationUnitLogoFile();
    insOperationUnit.setOperationUnitLogo(operationUnitLogo);
    result += fileHelper.updateBusinessFileData(operationUnitLogoFile,operationUnitLogo);
    //  公司照片
    String operationUnitImageFile = insOperationUnitDTO.getOperationUnitImageFile();
    insOperationUnit.setOperationUnitImage(operationUnitImage);
    result += fileHelper.updateBusinessFileData(operationUnitImageFile,operationUnitImage);
    //  更新业务数据
    result += insOperationUnitMapper.updateById(insOperationUnit);
    return result;
    }

    /**
     * 批量删除高速运营单位
     *
     * @param operationUnitIds 需要删除的高速运营单位主键
     * @return 结果
     */
    @Override
    public int deleteInsOperationUnitByOperationUnitIds(Long[] operationUnitIds)
    {
        return insOperationUnitMapper.deleteInsOperationUnitByOperationUnitIds(operationUnitIds);
    }

    /**
     * 删除高速运营单位信息
     *
     * @param operationUnitId 高速运营单位主键
     * @return 结果
     */
    @Override
    public int deleteInsOperationUnitByOperationUnitId(Long operationUnitId)
    {
        return insOperationUnitMapper.deleteInsOperationUnitByOperationUnitId(operationUnitId);
    }
}
